package com.pocketmenu.ui;

import java.util.ArrayList;
import java.util.List;

import com.pocketmenu.persistence.Ingredient;
import com.pocketmenu.persistence.IngredientDataSource;
import com.pocketmenu.persistence.PreferenceDataSource;


import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;

public class PreferencesActivity extends Activity {
	private PreferenceDataSource Pdatasource;
	private IngredientDataSource Idatasource;

	private List<String> ListaNombreIngredientes = new ArrayList<String>();
	
	
	private String listaCon="";
	
	 
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	Pdatasource = new PreferenceDataSource(this);
        Idatasource = new IngredientDataSource(this);
        Idatasource.open();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preferences);
        
        ListaNombreIngredientes=Idatasource.getAllNames();
        TextView tvCon=(TextView) findViewById(R.id.tvAgregadosCon);
        		
		Pdatasource.open();
		
       Cursor preferencias=Pdatasource.getNamesAndValid();
       preferencias.moveToFirst();
	    while (!preferencias.isAfterLast()) {
	    	
	    	boolean include = preferencias.getInt(0) != 0;
	    	String nombre =preferencias.getString(1);
	    	if(include){
	    		tvCon.setText(tvCon.getText()+nombre+",");
	    	}	    	
	      
	      preferencias.moveToNext();
	    }
	    // Make sure to close the cursor
	    preferencias.close();
        	
        
        
        
        ArrayAdapter<String> adapterIngredientes = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, ListaNombreIngredientes);
        AutoCompleteTextView autoCompleteCon = (AutoCompleteTextView)
                findViewById(R.id.actvCon);
        autoCompleteCon.setAdapter(adapterIngredientes);
        autoCompleteCon.setText("");
        autoCompleteCon.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				
				TextView tvCon=(TextView) findViewById(R.id.tvAgregadosCon);				
				if(!tvCon.getText().toString().contains(arg0.getItemAtPosition(arg2).toString())){
					tvCon.setText(tvCon.getText()+arg0.getItemAtPosition(arg2).toString()+",");					
				}
				AutoCompleteTextView autoCompleteCon2 = (AutoCompleteTextView)
		                findViewById(R.id.actvCon);
				autoCompleteCon2.setText("");
				
				
			}
        });
    }
    
    public void guardar(View v){
    	Pdatasource.deleteAll();    	
    	
    	TextView tvCon2=(TextView) findViewById(R.id.tvAgregadosCon);
    	
    	
    	String[] conIngredientes=tvCon2.getText().toString().split(",");
    	for(int j=0; j < conIngredientes.length;j++){
    		if(ListaNombreIngredientes.contains(conIngredientes[j])){
    			Ingredient buscado2=Idatasource.getIngredientByName(conIngredientes[j]);
    			if(buscado2!=null){
    				Pdatasource.create(buscado2.getId(), true);
    			}
    		}
    	}
    	
    	
    }   
}
